from django.shortcuts import render, redirect, reverse
from django.views import View
from django import http
from django.contrib.auth import login, authenticate, logout, mixins
from django.db import DatabaseError
from django_redis import get_redis_connection
from django.conf import settings

import logging
import re, json

from meiduo_mall.utils.response_code import RETCODE
from .models import User, Address
from .utils import generate_verify_email_url, check_token_to_user
from celery_tasks.email.tasks import send_verify_email
from meiduo_mall.utils.views import LoginRequiredView
from goods.models import SKU
from carts.utils import merge_cart_cookie_to_redis


"""创建日志输出器对象"""
logger = logging.getLogger('django')


"""账户注册界面"""
class RegisterView(View):
    def get(self, request):
        return render(request, 'register.html')


    def post(self, request):
        username = request.POST.get('username')
        password = request.POST.get('password')
        password2 = request.POST.get('password2')
        mobile = request.POST.get('mobile')
        sms_code = request.POST.get('sms_code')
        allow = request.POST.get('allow')

        if all([username, password, password2, mobile, sms_code, allow]) is False:
            return http.HttpResponseForbidden('缺少参数')

        if not re.match(r'^[a-zA-Z0-9_-]{5,20}$', username):
            return http.HttpResponseForbidden('请输入5-20个字符的用户名')

        if not re.match(r'^[0-9A-Za-z]{8,20}$', password):
            return http.HttpResponseForbidden('请输入8-20位的密码')

        if password != password2:
            return http.HttpResponseForbidden('两次输入的密码不一致')

        if not re.match(r'^1[345789]\d{9}$', mobile):
            return http.HttpResponseForbidden('您输入的手机号格式不正确')

        redis_conn = get_redis_connection('verify_code')
        sms_code_server = redis_conn.get('sms_%s' % mobile)
        if sms_code_server is None or sms_code != sms_code_server.decode():
            return http.HttpResponseForbidden('短信验证码有误')


        #创建一个用户用来记录一次登陆
        try:
            user = User.objects.create_user(
                username=username,
                password=password,  #密码在存储时需要加密后再存到表中
                mobile=mobile
            )
        except DatabaseError as e:
            logger.error(e)
            return render(request, 'register.html', {'register_errmsg':'用户注册失败'})

        #状态保持 request.session['id'] = user.id
        # 存储用户的ID到session中记录它的登陆状态
        login(request, user)

        return redirect('/')


"""查询当前用户名重复的个数"""
class UsernameCountView(View):
    def get(self, request, username):
        #查询当前用户名重复的个数，要么0要么1
        count = User.objects.filter(username=username).count()
        return http.JsonResponse({'count':count, 'code':RETCODE.OK, 'errmsg':'OK'})


"""查询当前电话号码重复"""
class MobileCountView(View):
    def get(self, request, mobile):
        # 查询当前电话号码重复的个数，要么0要么1
        count = User.objects.filter(mobile=mobile).count()
        return http.JsonResponse({'count': count, 'code': RETCODE.OK, 'errmsg': 'OK'})


"""账户登陆界面"""
class LoginView(View):

    def get(self,request):
        return render(request, 'login.html')

    def post(self,request):

        username = request.POST.get('username')
        password = request.POST.get('password')
        remembered = request.POST.get('remembered')

        if all([username, password]) is False:
            return http.HttpResponseForbidden('缺少必传参数')

        # user = User.objects.get(username=username)
        # user.check_password(password)


        # if re.match(r'^1[3-9]\d{9}$', username):
        #     User.USERNAME_FIELD = 'mobile'


        user = authenticate(username=username, password=password)
        if user is None:
            return render(request, 'login.html', {'account_errmsg':'用户名或密码错误'})

        if remembered != 'on':
            settings.SESSION_COOKIE_AGE = 0   #修改Django的SESSION的缓存时间

        #状态保持,默认是两周
        login(request, user)


        response = redirect(request.GET.get('next', '/'))
        response.set_cookie('username', user.username, max_age=settings.SESSION_COOKIE_AGE)

        # 登录成功那一刻合并购物车
        merge_cart_cookie_to_redis(request, response)

        return response
        # # 实现状态保持
        # login(request, user)
        # # 设置状态保持的周期
        # if remembered != 'on':
        #     # 没有记住用户：浏览器会话结束就过期, 默认是两周
        #     request.session.set_expiry(0)


"""退出登陆"""
class LogoutView(View):
    def get(self, request):

        logout(request)
        response = redirect(reverse('users:login'))
        response.delete_cookie('username')
        return response


"""用户中心"""
class UserInfoView(LoginRequiredView):
    def get(self, request):
        #先判断用户是否登陆，如果登陆返回用户中心界面
        #如果没有就返回登陆界面
        # user = request.user
        # if user.is_authenticated:
        #     return render(request, 'user_center_info.html')
        # else:
        #     return redirect('/login/?next=/info/')

        return render(request, 'user_center_info.html')


"""添加用户邮箱"""
class EmailView(LoginRequiredView):


    def put(self, request):
        #得到email数据
        json_dict = json.loads(request.body.decode())
        email = json_dict.get('email')

        if all([email]) is None:
            return http.HttpResponseForbidden('缺少邮箱数据')

        #校验
        if not re.match(r'^[a-z0-9][\w\.\-]*@[a-z0-9\-]+(\.[a-z]{2,5}){1,2}$', email):
            return http.HttpResponseForbidden('邮箱格式有误')

        #获取到user 设置user.email字段 调用save保存
        user = request.user
        user.email = email
        user.save()

        #在此地发送一个邮件到email
        #from django.core.mail import send_mail

        # send_mail(邮件主题, 普通邮件正文, 发件人邮箱, [收件人邮件], html_message='超文本邮件内容')
        # send_mail('美多', '', '美多商城<itcast99@163.com>', [email], html_message='收钱了')

        verify_url = generate_verify_email_url(user)
        send_verify_email.delay(email, verify_url)

        return http.JsonResponse({'rode':RETCODE.OK, 'errmsg':'OK'})


"""激活邮箱"""
class VerifyEmailView(View):

    def get(self,request):

        #获取token
        token = request.GET.get('token')

        #解密并获取到user
        user = check_token_to_user(token)
        if user is None:
            return http.HttpResponseForbidden('token无效')

        #修改当前user.email_active=true
        user.email_active = True
        user.save()

        #响应
        return redirect('/info/')


"""用户收货地址"""
class AddressView(LoginRequiredView):

    def get(self, request):

        # 提供用户收货地址界面
        # 获取当前用户的所有收货地址
        user = request.user

        # 获取当前用户的所有收货地址
        # address = user.addresses.filter(is_deleted=False)
        address_qs = Address.objects.filter(is_deleted=False, user=user)

        address_list = []
        for address in address_qs:
            address_dict = {
                'id': address.id,
                'title': address.title,
                'receiver': address.receiver,
                'province_id': address.province_id,
                'province': address.province.name,
                'city_id': address.city_id,
                'city': address.city.name,
                'district_id': address.district_id,
                'district': address.district.name,
                'place': address.place,
                'mobile': address.mobile,
                'tel': address.tel,
                'email': address.email,
            }
            address_list.append(address_dict)

        context = {
            'addresses': address_list,
            'default_address_id': user.default_address_id
        }

        return render(request, 'user_center_site.html', context)


"""新增用户收货地址"""
class CreateAddressView(LoginRequiredView):

    def post(self, request):

        user = request.user

        # 判断用户的收货地址数据,如果超过20个提前响应
        count = Address.objects.filter(user=user, is_deleted=False).count()
        # count = user.addresses.count()

        if count >= 20:
            return http.HttpResponseForbidden('用户收货地址上限')

        # 接收请求数据
        json_dict = json.loads(request.body.decode())

        title = json_dict.get('title')
        receiver = json_dict.get('receiver')
        province_id = json_dict.get('province_id')
        city_id = json_dict.get('city_id')
        district_id = json_dict.get('district_id')
        place = json_dict.get('place')
        mobile = json_dict.get('mobile')
        tel = json_dict.get('tel')
        email = json_dict.get('email')

        # 校验
        if all([title, receiver, province_id, city_id, district_id, place, mobile]) is False:
            return http.HttpResponseForbidden('缺少必传参数')

        if not re.match(r'^1[3-9]\d{9}$', mobile):
            return http.HttpResponseForbidden('参数mobile有误')
        if tel:
            if not re.match(r'^(0[0-9]{2,3}-)?([2-9][0-9]{6,7})+(-[0-9]{1,4})?$', tel):
                return http.HttpResponseForbidden('参数tel有误')
        if email:
            if not re.match(r'^[a-z0-9][\w\.\-]*@[a-z0-9\-]+(\.[a-z]{2,5}){1,2}$', email):
                return http.HttpResponseForbidden('参数email有误')

        # 新增
        try:
            address = Address.objects.create(
                user=user,
                title=title,
                receiver=receiver,
                province_id=province_id,
                city_id=city_id,
                district_id=district_id,
                place=place,
                mobile=mobile,
                tel=tel,
                email=email
            )
            if user.default_address is None:    # 判断当前用户是否有默认收货地址
                user.default_address = address  # 就把当前的收货地址设置为它的默认值
                user.save()
        except Exception:
            return http.HttpResponseForbidden('新增地址出错')

        # 把新增的地址数据响应回去
        address_dict = {
            'id': address.id,
            'title': address.title,
            'receiver': address.receiver,
            'province_id': address.province_id,
            'province': address.province.name,
            'city_id': address.city_id,
            'city': address.city.name,
            'district_id': address.district_id,
            'district': address.district.name,
            'place': address.place,
            'mobile': address.mobile,
            'tel': address.tel,
            'email': address.email,
        }
        return http.JsonResponse({'rode': RETCODE.OK, 'errmsg': 'OK', 'address': address_dict})


"""地址修改和删除"""
class UpdateDestroyAddressView(LoginRequiredView):

    def put(self, request, address_id):

        # 查询要修改的地址对象
        try:
            address = Address.objects.get(id=address_id)
        except Address.DoesNotExist:
            return http.HttpResponseForbidden('要修改的地址不存在')

        # 接收
        json_dict = json.loads(request.body.decode())

        title = json_dict.get('title')
        receiver = json_dict.get('receiver')
        province_id = json_dict.get('province_id')
        city_id = json_dict.get('city_id')
        district_id = json_dict.get('district_id')
        place = json_dict.get('place')
        mobile = json_dict.get('mobile')
        tel = json_dict.get('tel')
        email = json_dict.get('email')

        # 校验
        if all([title, receiver, province_id, city_id, district_id, place, mobile]) is False:
            return http.HttpResponseForbidden('缺少必传参数')

        if not re.match(r'^1[3-9]\d{9}$', mobile):
            return http.HttpResponseForbidden('参数mobile有误')
        if tel:
            if not re.match(r'^(0[0-9]{2,3}-)?([2-9][0-9]{6,7})+(-[0-9]{1,4})?$', tel):
                return http.HttpResponseForbidden('参数tel有误')
        if email:
            if not re.match(r'^[a-z0-9][\w\.\-]*@[a-z0-9\-]+(\.[a-z]{2,5}){1,2}$', email):
                return http.HttpResponseForbidden('参数email有误')

        # 修改
        Address.objects.filter(id=address_id).update(
            title=title,
            receiver=receiver,
            province_id=province_id,
            city_id=city_id,
            district_id=district_id,
            place=place,
            mobile=mobile,
            tel=tel,
            email=email
        )
        address = Address.objects.get(id=address_id)  # 要重新查询一次新数据

        # 把新增的地址数据响应回去
        address_dict = {
            'id': address.id,
            'title': address.title,
            'receiver': address.receiver,
            'province_id': address.province_id,
            'province': address.province.name,
            'city_id': address.city_id,
            'city': address.city.name,
            'district_id': address.district_id,
            'district': address.district.name,
            'place': address.place,
            'mobile': address.mobile,
            'tel': address.tel,
            'email': address.email,
        }
        return http.JsonResponse({'code': RETCODE.OK, 'errmsg': 'OK', 'address': address_dict})

    """对收货地址逻辑删除"""
    def delete(self, request, address_id):

        try:
            address = Address.objects.get(id=address_id)
        except Address.DoesNotExist:
            return http.HttpResponseForbidden('要删除的地址不存在')

        address.is_deleted = True

        address.save()

        return http.JsonResponse({'code': RETCODE.OK, 'errmsg': 'OK'})


"""设置默认地址"""
class DefaultAddressView(LoginRequiredView):

    def put(self, request, address_id):

        try:
            address = Address.objects.get(id=address_id)
        except Address.DoesNotExist:
            return http.HttpResponseForbidden('要修改的地址不存在')

        user = request.user
        user.default_address = address
        user.save()

        return http.JsonResponse({'code': RETCODE.OK, 'errmsg': 'OK'})


"""修改用户收货地址标题"""
class UpdateTitleAddressView(LoginRequiredView):

    def put(self, request, address_id):

        try:
            address = Address.objects.get(id=address_id)
        except Address.DoesNotExist:
            return http.HttpResponseForbidden('要修改的地址不存在')

        json_dict = json.loads(request.body.decode())
        title = json_dict.get('title')
        address.title = title
        address.save()

        return http.JsonResponse({'rode': RETCODE.OK, 'errmsg': 'OK'})


"""修改密码"""
class ChangePasswordView(LoginRequiredView):

    def get(self, request):
        return render(request, 'user_center_pass.html')

    def post(self, request):

        # 接收参数
        old_password = request.POST.get('old_pwd')
        password = request.POST.get('new_pwd')
        password2 = request.POST.get('new_cpwd')

        # 校验
        if all([old_password, password, password2]) is False:
            return http.HttpResponseForbidden("缺少必传参数")

        user = request.user
        if user.check_password(old_password) is False:
            return render(request, 'user_center_pass.html', {'origin_pwd_errmsg': '原始密码错误'})

        if not re.match(r'^[0-9A-Za-z]{8,20}$', password):
            return http.HttpResponseForbidden('密码最少8位，最长20位')
        if password != password2:
            return http.HttpResponseForbidden('两次输入的密码不一致')

        # 修改密码
        user.set_password(password)
        user.save()

        # 响应重定向到登录界面
        logout(request)
        response = redirect('/login/')
        response.delete_cookie('username')

        return response


"""用户商品浏览记录"""
class UserBrowseHistory(View):

    """获得浏览记录"""
    def post(self, request):

        #判断当前用户是否登陆
        user = request.user
        if not user.is_authenticated:
            return http.JsonResponse({'code': RETCODE.SESSIONERR, 'errmsg': '用户未登录'})

        #获取请求体中的sku_id
        json_dict = json.loads(request.body.decode())
        sku_id = json_dict.get('sku_id')

        #校验sku_id
        try:
            sku = SKU.objects.get(id=sku_id)
        except SKU.DoesNotExist:
            return http.HttpResponseForbidden('sku_id不存在')

        #创建redis连接对象
        redis_conn = get_redis_connection('history')

        pl = redis_conn.pipeline()

        key = 'history_%s' % user.id

        pl.lrem(key, 0, sku_id)  #去重

        pl.lpush(key,sku_id)     #存储到列表

        pl.ltrim(key, 0, 4)      #存储的个数，前五个

        #执行管道
        pl.execute()

        #响应
        return http.JsonResponse({'code': RETCODE.OK, 'errmsg': 'OK'})

    """浏览记录查询"""
    def get(self,requst):

        #创建redis链接对象
        redis_conn = get_redis_connection('history')
        sku_id_list = redis_conn.lrange('history_%s' % requst.user.id, 0, -1)

        #获取浏览记录列表数据
        skus = []

        for sku_id in sku_id_list:
            sku = SKU.objects.get(id=sku_id)

        #通过sku_id查询sku,再将sku模型转换成字典
            sku_dict = {
                'id': sku.id,
                'name': sku.name,
                'default_image_url': sku.default_image.url,
                'price':sku.price
            }

            skus.append(sku_dict)

        return http.JsonResponse({'code': RETCODE.OK, 'errmsg': 'OK', 'skus': skus})







